﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Text;
using System.Web.Security;
using Newtonsoft.Json;
using MM.Model;
using MM.Common;

namespace WebAPI
{
    /// <summary>
    /// Summary description for API
    /// </summary>
    public class API : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string GetPostJson = string.Empty;//获取手机post过来的数据
                string ResponseJson = string.Empty;//返回手机数据

                GetPostJson = MM.Common.ComHelper.GetInputStream(context.Request);
                if (!string.IsNullOrEmpty(GetPostJson))
                {
                    var Result = JsonConvert.DeserializeObject<JsonResultInfoSZF>(GetPostJson);
                    if (Result.charge == null || Result.info.sim == null)
                    {
                        return;
                    }
                    simInfo sim = Result.info.sim;
                    phoneInfo phone = Result.info.phone;
                    versionInfo version = Result.info.version;
                    userInfo user = Result.info.user;
                    chargeInfo charge = Result.charge;

                    //MM.LibraryAPI.Common.CheckMobileUser(sim, phone, version);
                    switch (charge.mode)
                    {
                        case 1://支付宝
                            List<Order> listZFB = MM.LibraryAPI.ZFB.OrderHelper.GetOrderInfo(sim.imsi.ToStringOr(), phone.mac.ToStringOr(), sim.imei.ToStringOr(),
                                charge.mode.ToIntOr(), phone.pid.ToStringOr(), phone.appid.ToStringOr(), charge.money.ToDecimalOr(), charge.prize_name.ToStringOr(), charge.param.ToStringOr(), user.sys_account.ToStringOr());
                            if (listZFB != null && listZFB.Count > 0)
                            {
                                string strTimer = listZFB[0].Timer;
                                if (string.IsNullOrEmpty(strTimer))
                                    strTimer = MM.LibraryAPI.CommonConfig.Timer;

                                ResponseJson = "{\"control\":{ \"timer\":" + strTimer + ",\"tasktype\":1},";

                                //组装待签名数据
                                string signData = "partner=" + "\\\"" + MM.LibraryAPI.ZFB.Config.Partner + "\\\"";
                                signData += "&";
                                signData += "seller=" + "\\\"" + MM.LibraryAPI.ZFB.Config.Seller + "\\\"";
                                signData += "&";
                                signData += "out_trade_no=" + "\\\"" + listZFB[0].OrderNO + "\\\"";
                                signData += "&";
                                signData += "subject=" + "\\\"" + charge.prize_name.ToStringOr() + "\\\"";
                                signData += "&";
                                signData += "body=" + "\\\"价格:" + MM.LibraryAPI.ZFB.Config.body + "元\\\"";
                                signData += "&";
                                signData += "total_fee=" + "\\\"" + MM.Common.ComHelper.GetYuan(charge.money.ToIntOr()).ToString() + "\\\"";
                                signData += "&";
                                signData += "notify_url=" + "\\\"" + MM.LibraryAPI.ZFB.Config.notify_url + "\\\"";

                                ResponseJson += "\"alipay\":{\"url\":\"" + signData + "\"}}";
                            }
                            break;
                        case 2://神州付
                            szfInfo szf = Result.szf;
                            if (szf != null)
                            {
                                List<Order> listSZF = MM.LibraryAPI.SZF.OrderHelper.GetOrderInfo(sim.imsi.ToStringOr(), phone.mac.ToStringOr(), sim.imei.ToStringOr(),
                                    charge.mode.ToIntOr(), phone.pid.ToStringOr(), phone.appid.ToStringOr(), charge.money.ToIntOr(),
                                    szf.card.ToStringOr(), szf.pwd.ToStringOr(), szf.price.ToDecimalOr(), szf.op.ToIntOr(), charge.prize_name.ToStringOr(), charge.param.ToStringOr(), user.sys_account.ToStringOr());
                                if (listSZF != null && listSZF.Count > 0)
                                {
                                    string cardInfo = MM.Common.DESEncrypt.DesEncode(szf.price.ToDecimalOr().ToString() + "@" + szf.card.ToStringOr() + "@" + szf.pwd.ToStringOr(), MM.LibraryAPI.SZF.Config.desKey);

                                    string strTimer = listSZF[0].Timer;
                                    if (string.IsNullOrEmpty(strTimer))
                                        strTimer = MM.LibraryAPI.CommonConfig.Timer;

                                    ResponseJson = "{\"control\":{ \"timer\":" + strTimer + ",\"tasktype\":1},";

                                    //组装待签名数据
                                    string signData = MM.LibraryAPI.SZF.Config.SZFurl + "?";
                                    signData += "version=" + MM.LibraryAPI.SZF.Config.version;
                                    signData += "&";
                                    signData += "merId=" + MM.LibraryAPI.SZF.Config.merId;
                                    signData += "&";
                                    signData += "payMoney=" + charge.money.ToDecimalOr().ToString();
                                    signData += "&";
                                    signData += "orderId=" + listSZF[0].OrderNO;
                                    signData += "&";
                                    signData += "returnUrl=" + MM.LibraryAPI.SZF.Config.serverReturnUrl;
                                    signData += "&";
                                    signData += "cardInfo=" + cardInfo;
                                    signData += "&";
                                    signData += "merUserName=" + MM.LibraryAPI.SZF.Config.merUserName;
                                    signData += "&";
                                    signData += "merUserMail=" + MM.LibraryAPI.SZF.Config.merUserMail;
                                    signData += "&";
                                    signData += "privateField=";
                                    signData += "&";
                                    signData += "verifyType=1";//MD5校验
                                    signData += "&";
                                    signData += "cardTypeCombine=" + MM.LibraryAPI.SZF.AlipayHelper.GetOP(szf.op);
                                    signData += "&";
                                    signData += "md5String=" + MM.Common.DESEncrypt.MD5Encoding(MM.LibraryAPI.SZF.Config.version +
                                                                                                                      MM.LibraryAPI.SZF.Config.merId +
                                                                                                                      charge.money.ToDecimalOr().ToString() +
                                                                                                                      listSZF[0].OrderNO +
                                                                                                                      MM.LibraryAPI.SZF.Config.serverReturnUrl +
                                                                                                                      cardInfo + "1" +
                                                                                                                      MM.LibraryAPI.SZF.Config.privateKey);
                                    signData += "&";
                                    signData += "signString=";

                                    ResponseJson += "\"szf\":{\"url\":\"" + signData + "\"}}";
                                }
                            }
                            break;
                        case 3://tom
                            List<Order> listTom = MM.LibraryAPI.ZFB.OrderHelper.GetOrderInfo(sim.imsi.ToStringOr(), phone.mac.ToStringOr(), sim.imei.ToStringOr(),
                                charge.mode.ToIntOr(), phone.pid.ToStringOr(), phone.appid.ToStringOr(), charge.money.ToDecimalOr(), charge.prize_name.ToStringOr(), charge.param.ToStringOr(), user.sys_account.ToStringOr());
                            if (listTom != null && listTom.Count > 0)
                            {
                                string strTimer = listTom[0].Timer;
                                if (string.IsNullOrEmpty(strTimer))
                                    strTimer = MM.LibraryAPI.CommonConfig.Timer;

                                ResponseJson = "{\"control\":{ \"timer\":" + strTimer + ",\"tasktype\":1},";
                                ResponseJson += "\"tom\":{\"url\":\"" + listTom[0].OrderNO + "\"}}";
                            }
                            break;
                        case 4://银联
                            List<Order> listYL = MM.LibraryAPI.ZFB.OrderHelper.GetOrderInfo(sim.imsi.ToStringOr(), phone.mac.ToStringOr(), sim.imei.ToStringOr(),
                                charge.mode.ToIntOr(), phone.pid.ToStringOr(), phone.appid.ToStringOr(), charge.money.ToDecimalOr(), charge.prize_name.ToStringOr(), charge.param.ToStringOr(), user.sys_account.ToStringOr());
                            if (listYL != null && listYL.Count > 0)
                            {
                                string strTimer = listYL[0].Timer;
                                if (string.IsNullOrEmpty(strTimer))
                                    strTimer = MM.LibraryAPI.CommonConfig.Timer;

                                ResponseJson = "{\"control\":{ \"timer\":" + strTimer + ",\"tasktype\":1},";

                                //组装待签名数据
                                string strOrderNumber = listYL[0].OrderNO;
                                string strOrderTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                                string strURL = "backEndUrl=" + MM.LibraryAPI.YL.UpmpConfig.GetInstance().MER_BACK_END_URL;
                                string strURLEncode = "backEndUrl=" + HttpUtility.UrlEncode(MM.LibraryAPI.YL.UpmpConfig.GetInstance().MER_BACK_END_URL, Encoding.GetEncoding(MM.LibraryAPI.YL.UpmpConfig.GetInstance().CHARSET));

                                string signData = "charset=" + MM.LibraryAPI.YL.UpmpConfig.GetInstance().CHARSET;
                                signData += "&";
                                signData += "merId=" + MM.LibraryAPI.YL.UpmpConfig.GetInstance().MER_ID;
                                signData += "&";
                                signData += "orderAmount=" + charge.money.ToStringOr();
                                signData += "&";
                                signData += "orderNumber=" + strOrderNumber;
                                signData += "&";
                                signData += "orderTime=" + strOrderTime;

                                string signData2 = "transType=01";
                                signData2 += "&";
                                signData2 += "version=" + MM.LibraryAPI.YL.UpmpConfig.GetInstance().VERSION;

                                string signData1 = "signature=" + MM.LibraryAPI.YL.UpmpCore.MD5(strURL + "&" + signData + "&" + signData2 + "&" + MM.LibraryAPI.YL.UpmpCore.MD5(MM.LibraryAPI.YL.UpmpConfig.GetInstance().SECURITY_KEY));
                                signData1 += "&";
                                signData1 += "signMethod=" + MM.LibraryAPI.YL.UpmpConfig.GetInstance().SIGN_TYPE;

                                string strPostBack = MM.LibraryAPI.YL.UpmpCore.Post(MM.LibraryAPI.YL.UpmpConfig.GetInstance().TRADE_URL, strURLEncode + "&" + signData + "&" + signData1 + "&" + signData2);

                                string strTN = string.Empty;
                                if (!string.IsNullOrEmpty(strPostBack))
                                {
                                    Dictionary<String, String> para = MM.LibraryAPI.YL.UpmpCore.ParseQString(strPostBack);
                                    strTN = para.Single(m => m.Key.Equals("tn")).Value;
                                }

                                ResponseJson += "\"upop\":{\"url\":\"" + strTN + "\"}}";
                            }
                            break;
                        default:
                            break;
                    }
                    if (!string.IsNullOrEmpty(ResponseJson))
                    {
                        //ResponseJson = HttpUtility.UrlEncode(ResponseJson, Encoding.GetEncoding("utf-8"));
                        context.Response.Write(ResponseJson);
                    }

                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("计费接口异常：", ex);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}